package iTw3etPackage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

public class OAuthTwitterClient {
	private final static String consumerKey = "Wajm4xOUbeB52k89CrCQ";
	private final static String consumerSecret = "cKbpBRSUTr2YQFiZo7zeC6U6cnE9xywznAkAfejg";

	private final static String fileToken = "iTw3etToken.txt";

	public void updateStatus(String status) throws TwitterException,
			IOException {
		
		Twitter twitter = new Twitter();
		twitter.setOAuthConsumer(consumerKey, consumerSecret);
		RequestToken requestToken = twitter.getOAuthRequestToken();
		AccessToken accessToken = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (accessToken == null) {
			System.out
					.println("Open the following URL and grant access to your account:");
			System.out.println(requestToken.getAuthorizationURL());
			// Copy the authorization URL in your browser
			// http://twitter.com/oauth/authorize?oauth_token=r9YJ8P1gZJVzmb8Y6pYSsosUYe2o8MRCchuFVrspoRU
			// Log in Twitter, grant access
			// Get a PIN returned, e.g., 738805
			// Notice you can use HttpClient to do the job above
			System.out
					.println("Copy the PIN displayed in your browser, then hit ENTER: ");
			String pin = br.readLine();
			accessToken = twitter.getOAuthAccessToken(requestToken, pin);
		}

		// Persist the access token for future reference
		int userId = twitter.verifyCredentials().getId();
		storeAccessToken(userId, accessToken);
		Status twitterStatus = twitter.updateStatus(status);
		System.out.println("Successfully updated the status for user with ID "
				+ userId + " to [" + twitterStatus.getText() + "].");
	}

	public void updateStatus(int userId, String status)
			throws TwitterException, IOException {
		Twitter twitter = new Twitter();
		twitter.setOAuthConsumer(consumerKey, consumerSecret);
		AccessToken accessToken = loadAccessToken(userId);
		twitter.setOAuthAccessToken(accessToken);
		Status twitterStatus = twitter.updateStatus(status);
		System.out.println("Successfully updated the status for user with ID "
				+ userId + " to [" + twitterStatus.getText() + "].");
	}

	private void storeAccessToken(int userId, AccessToken accessToken)
			throws IOException {
		// Store user ID, token and token secret
		System.out.print("Storing access token for " + userId + "...");
		BufferedWriter out = new BufferedWriter(new FileWriter(fileToken));
		out.append("" + userId);
		out.append(" ");
		out.append(accessToken.getToken());
		out.append(" ");
		out.append(accessToken.getTokenSecret());
		out.newLine();
		out.close();
		System.out.println("Done");
	}

	private AccessToken loadAccessToken(int userId) throws IOException {
		
		AccessToken accessToken = null;
		String id = "" + userId;
		BufferedReader in = new BufferedReader(new FileReader(fileToken));

		String line;
		String[] fields;
		while ((line = in.readLine()) != null) {
			fields = line.split(" ");
			if (id.equals(fields[0])) {
				accessToken = new AccessToken(fields[1], fields[2]);
				System.out.println("Access token found for " + userId);
				break;
			}
		}

		in.close();

		
		/*
		String twitterToken="215323632-jKvI6qx70DRIHwfGqAViEI57XgykPWOI0bfI2BI4";
		String twitterTokenSecret="hrQfEo4K9Pj9ScKJlJ0b3YjeHaG5esrARiQV9bWucs";
		
		AccessToken accessToken = null;
		accessToken = new AccessToken(twitterToken, twitterTokenSecret);
		*/
		return accessToken;
	}
	
	/**
	 * 43319040 - xF1r3x
	 * 215323632 - 	iTw3et
	 * 15232572 - webauth
	 */

	/**
	 * @param args
	 * @throws TwitterException
	 * @throws IOException
	 */
	public static void main(String[] args) throws TwitterException, IOException {
		OAuthTwitterClient tt = new OAuthTwitterClient();
		//tt.updateStatus("No Idea what this means");
		// REPLACE 23741179 WITH YOUR OWN TWITTER ID
		
		tt.updateStatus(215323632, "18Nov2010 Update");
			
	}

}
